<?php
/**
 * FooDB.php
 * Wrapper class for mysqli calls
 * Based on JawsDB from the Jaws Project (http://jaws-project.com)
 *
 * $Id: FooDB.php 14 2007-08-08 06:08:41Z jonathan.hernandez $
 */
class FooDB {

    private $dbh;
    private $dbconf;

    function __construct($dbconf) {
        $this->dbconf = $dbconf;
    }

    public function connect() 
    {
        $this->dbh = new mysqli($this->dbconf['host'], 
                                $this->dbconf['user'], 
                                $this->dbconf['password'], 
                                $this->dbconf['name']);

        if (mysqli_connect_errno()) {
            throw new Exception("Connect failed: ". mysqli_connect_error());
        }
    }

    public function close() 
    {
        $this->dbh->close();
    }
    
    private function sqlParse($sql, $params = null) 
    {
        if (is_array($params)) {
            foreach ($params as $key => $value) {
                if ($value === 'NULL') {
                    $sql = str_replace('{'.$key.'}', 'NULL', $sql);
                } else {
                    if (is_int($value)) {
                        $value = $this->dbh->real_escape_string($value); 
                    } elseif (is_bool($value)) {
                        $value = $value?'TRUE':'FALSE';
                    } else {
                        $value = "'".$this->dbh->real_escape_string($value)."'"; 
                    }
                    $sql = str_replace('{'.$key.'}', $value, $sql);
                }
            }
        }
        return $sql;
    }

    public function query($sql, $params = null)
    {
        $sql = $this->sqlParse($sql, $params);
        if (!$result = $this->dbh->query($sql)) {
            throw new Exception("Query failed: ". $this->dbh->error);
        }

        return (string)$result;
    }

    public function queryRow($sql, $params = null)
    {
        $sql = $this->sqlParse($sql, $params);
        
        if (!$result = $this->dbh->query($sql)) {
            throw new Exception("Query failed: ". $this->dbh->error);
        }
        
        $row = $result->fetch_assoc();
        
        if (!$row || !is_array($row)) {
            return false;
        }
        
        return $row;
    }

    public function queryAll($sql, $params)
    {
        $sql = $this->sqlParse($sql, $params);
        if (!$result = $this->dbh->query($sql)) {
            throw new Exception("Query failed");
        }

        $res = array();
        
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $res[] = $row;
            }
        }
        return $res;
    }

    public function queryOne($sql, $params = null, $colnum = 0)
    {
        $sql = $this->sqlParse($sql, $params);
        if (!$result = $this->dbh->query($sql)) {
            throw new Exception("Query failed: ". $this->dbh->error);
        }

        $row = $result->fetch_row();
        
        if (!$row || !is_array($row)) {
            return false;
        }
        return $row[$colnum];
    }

}
